package com.share.commons.util;

import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.cfg.AnnotationConfiguration;
import org.hibernate.cfg.Configuration;

/**
 * <p>Title：Hibernate工具类</p>
 * <p>Description：产生SessionFactory和Session</p>
 * @Author 杨浩泉
 * @Date 2010-2-23
 */
public class HibernateUtil {
	
	private static SessionFactory sessionFactory = null ;
	/*
	public static void main(String[] args) {
		System.out.println(HibernateUtil.getSession()) ;
	}
	*/
	static {
		Configuration cfg = new AnnotationConfiguration().configure() ;
		sessionFactory = cfg.buildSessionFactory() ;
	}
	
	public static SessionFactory getSessionFactory() {
		return sessionFactory ;
	}
	
	public static Session getSession() {
		return sessionFactory.getCurrentSession() ;
	}
	
	public static void closeSession(Session session) {
		if(session!=null) {
			session.flush() ;
			session.close() ;
		}
	}
	
	public static void closeSessionFactory(SessionFactory sf) {
		if(sf!=null) {
			sf.close() ;
		}
	}

}
